<template>
  <van-nav-bar title="注册" />

  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
        v-model="zhuceFrom.username"
        name="手机号"
        label="手机号"
        placeholder="手机号"
        :rules="[{ pattern, message: '请填写手机号' }]"
      />
      <van-field
        v-model="zhuceFrom.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ pattern, message: '请填写密码' }]"
      />
      <van-field
        v-model="zhuceFrom.passwordOK"
        type="password"
        name="password"
        label="确认密码"
        placeholder="确认密码"
        :rules="[{ validator, message: '请再次填写密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px">
      <van-button
        round
        block
        type="primary"
        native-type="submit"
        @click="submit"
      >
        注册
      </van-button>
    </div>
  </van-form>
</template>

<script setup>
import { zhuceApi } from "@/api/api";
import { reactive } from "vue";
import { showToast } from "vant";
import { useRouter } from "vue-router";
let router = new useRouter();
let zhuceFrom = reactive({
  username: "",
  password: "",
  passwordOK: "",
});
let validator = (val) => {
  if (val != zhuceFrom.password) {
    return false;
  }
  return true;
};
let submit = () => {
  zhuceApi(zhuceFrom.username, zhuceFrom.password).then((res) => {
    console.log("注册", res);
    if (res.data.code == 0) {
      showToast({
        message: "注册成功",
        type:'success',
        duration:'2000',
      });
      router.push('/denglu')
    }
    else{
      showToast({
        message: "注册失败",
        type:'fail',
        duration:'2000',
      });
    }
  });
};
</script>

<style lang="scss" scoped>
</style>